package org.example.collection.collection;

import java.util.*;

public class Demo1 {
    public static void main(String[] args) {
//        test1();
//        test2();
//        test3();
        test4();
    }

    private static void test4() {
        ArrayList<Person> arrayList = new ArrayList<>();
        arrayList.add(new Person("李四", 19, "女"));
        arrayList.add(new Person("张三", 22, "男"));
        arrayList.add(new Person("王五", 34, "男"));
        arrayList.add(new Person("赵六", 30, "男"));
        arrayList.add(new Person("田七", 25, "女"));
        ListIterator<Person> listIterator = arrayList.listIterator();

//        listIterator.remove(); // 报错，初始保留的索引为-1
        listIterator.next();
        listIterator.remove(); // 正常
        listIterator.next();
        listIterator.add(new Person("aa", 21, "女"));    // 正常
//        listIterator.remove(); // 报错
    }

    private static void test3() {
        ArrayList<Person> arrayList = new ArrayList<>();
        arrayList.add(new Person("李四", 19, "女"));
        arrayList.add(new Person("张三", 22, "男"));
        arrayList.add(new Person("王五", 34, "男"));
        arrayList.add(new Person("赵六", 30, "男"));
        arrayList.add(new Person("田七", 25, "女"));
        ListIterator<Person> listIterator = arrayList.listIterator();
        System.out.println(listIterator.hasNext()); // true
        System.out.println(listIterator.hasPrevious()); // false
        listIterator.forEachRemaining(System.out::println);
        System.out.println(listIterator.hasNext()); // false
        System.out.println(listIterator.hasPrevious()); // true
    }

    public static void test2() {
        Collection<Person> collection1 = new ArrayList<>();
        // add
        Person person1 = new Person("李四", 19, "女");
        collection1.add(person1);
        collection1.add(new Person("张三", 22, "男"));
        Iterator<Person> iterator = collection1.iterator();
        iterator.forEachRemaining(System.out::println);

        for(Person person: collection1){
            if (person.getAge() == 19)
                collection1.add(new Person("田七", 25, "女"));
        }

    }

    public static void test1(){
        Collection<Person> collection = new ArrayList<>();
        // add
        Person person1 = new Person("李四", 19, "女");
        collection.add(person1);
        collection.add(new Person("张三", 22, "男"));
        collection.add(new Person("王五", 34, "男"));
        collection.add(new Person("赵六", 30, "男"));
        collection.add(new Person("田七", 25, "女"));
        // size
        System.out.println(collection.size());  // 5
        // remove
        collection.remove(person1);
        // contains
        // false(Person不是值对象而是实体，就算属性相同也不是同一个实体)
        System.out.println(collection.contains(person1));
        // removeIf, 避免了在使用循环迭代操作时无法对集合进行修改
        collection.removeIf(
                person -> person.getAge() >= 30
        );
        // clear, isEmpty
        collection.clear();
        System.out.println(collection.isEmpty());   // ture
    }

    static class Person {
        private final String name;//姓名
        private final Integer age;//年龄
        private final String gender;//性别

        public Person(String name, Integer age, String gender) {
            this.name = name;
            this.age = age;
            this.gender = gender;
        }

        //重写toString，方便观看结果
        @Override
        public String toString() {
            return "Person{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    ", gender='" + gender + '\'' +
                    '}';
        }

        public int getAge() {
            return age;
        }
    }
}
